/**成就勋章类 */
class achievementMedal extends eui.Component {
	//---------------------------------层级
	/**背景层 */
	private backgroundLayer: eui.Component;
	/**勋章层 */
	private medalLayer: eui.Component;
	/**皮肤层 */
	public skinLayer: eui.Component;

	//---------------------------------图片
	/**没获取到勋章的图片地址 */
	private noGetMedalName: string;
	/**获取到勋章的图片地址 */
	private getMedalName: string;
	/**成就对应的商品地址 */
	private medalToShopName: string;

	//---------------------------------属性
	/**对应商品ID */
	public shopId: number;
	/**商品类型 */
	public shopType: number;
	/**商品名字 */
	private shopName: string;
	/**状态 
	 * 0-未领取
	 * 1-待领取
	 * 2-未穿戴
	 * 3-穿戴
	*/
	public states: number;
	/**介绍信息 */
	public introduce: string;
	/**进度下标 */
	public schedule: number;
	/**成就名字 */
	public medalName: string;


	/**点击商品动作 */
	private GoodsAction: string[] = ["chuandai", "xiexia"];

	public constructor(data) {
		super();
		this.init(data);
	}
	/**勋章初始化 */
	private init(data) {
		this.shopId = parseInt(data.shopId);
		this.states = parseInt(data.states);
		this.shopType = parseInt(data.shopType);
		this.medalToShopName = data.shopResName;

		let goodNameJson = RES.getRes("medal_json");
		let shopData = this.getGoodData(goodNameJson);
		this.shopName = shopData.shopName;
		this.noGetMedalName = shopData.medalResName;
		this.introduce = shopData.introduce;
		this.schedule = shopData.number;
		this.medalName = shopData.medalName;
		this.getMedalName = "medal_box";

		this.medalLayer = new eui.Component();
		this.skinLayer = new eui.Component();
		this.backgroundLayer = new eui.Component();
		this.addChild(this.backgroundLayer);
		let medal_beiJing: eui.Image = this.loadImg("bantouming", 0, 0, 160, 200);
		//medal_beiJing.alpha=0;
		this.backgroundLayer.addChild(medal_beiJing);
		this.addChild(this.medalLayer);
		this.addChild(this.skinLayer);
		this.goodShow();
	}
	/**物品显示 */
	public goodShow() {
		switch (this.states) {
			case 0:
				this.noGetMedalShow();
				break;
			case 1:
				this.getMedalShow();
				break;
			case 2:
			case 3:
				this.shopShow();
				break;
			default:
				break;
		}

	}
	/**获取商品数据 */
	private getGoodData(goodData) {
		for (let i = 0; i < goodData.length; i++) {
			if (goodData[i].shopId == this.shopId) {
				return goodData[i];
			}
		}

	}
	/**加载图片 */
	public loadImg(resName, x, y, width, height) {
		var Image: eui.Image = new eui.Image();
		Image.texture = RES.getRes(resName);
		Image.width = width;
		Image.height = height;
		Image.x = x;
		Image.y = y;
		Image.anchorOffsetX = Image.width / 2;
		Image.anchorOffsetY = Image.height / 2;
		return Image;
	}
	/**加载文字 */
	private loadLabel(content: string, _x: number, _y: number, width: number, heigth: number, rotation: number, size: number): any {
		var Label: eui.Label = new eui.Label(content);
		Label.x = _x;
		Label.y = _y;
		Label.width = width;
		Label.height = heigth;
		Label.rotation = rotation;
		Label.size = size;
		Label.anchorOffsetX = Label.width / 2;
		Label.anchorOffsetY = Label.height / 2;
		Label.textAlign = egret.HorizontalAlign.CENTER;
		Label.verticalAlign = egret.VerticalAlign.MIDDLE;
		Label.fontFamily = "Microsoft YaHei";
		return Label;
	}
	/**重置勋章层/皮肤层 */
	private restAllLayer() {
		if (this.medalLayer.numChildren > 0) {
			this.medalLayer.removeChildren();
		}
		if (this.skinLayer.numChildren > 0) {
			this.skinLayer.removeChildren();
		}
	}

	/**没有获取到勋章状态 */
	public noGetMedalShow() {
		this.restAllLayer();
		let medal_hui: eui.Image = this.loadImg(this.noGetMedalName, 0, -5, 104, 118);
		this.medalLayer.addChild(medal_hui);
		this.achievementsProgress();
	}
	/**加载成就进度 */
	public achievementsProgress() {
		/**成就进度 */
		let min = 0;
		/**成就上线 */
		let max = 1;
		if (this.schedule >= 0 && this.schedule < 5) {
			switch (this.schedule) {
				case 0:
					min = parseInt(GlobalData.usrRecorData.KillNumber);
					max = parseInt(GlobalData.attainmentTargets.KillNumber);
					break;
				case 1:
					min = parseInt(GlobalData.usrRecorData.LineLength);
					max = parseInt(GlobalData.attainmentTargets.LineLength);
					break;
				case 2:
					min = parseInt(GlobalData.usrRecorData.InvoteFriends);
					max = parseInt(GlobalData.attainmentTargets.InvoteFriends);
					break;
				case 3:
					min = parseInt(GlobalData.usrRecorData.Weight);
					max = parseInt(GlobalData.attainmentTargets.Weight);
					break;
				case 4:
					min = parseInt(GlobalData.usrRecorData.VipNumbers);
					max = parseInt(GlobalData.attainmentTargets.VipNumbers);
					break;
				default:
					break;
			}
		}
		/**进度条 */
		let jinDu: eui.Image = this.loadImg("medal_tiao", -59, 80, min / max * 113, 6);
		jinDu.anchorOffsetX = 0;
		jinDu.anchorOffsetY = 0;
		/**进度条背景 */
		let di: eui.Image = this.loadImg("medal_di", -61, 79, 115, 8);
		di.anchorOffsetX = 0;
		di.anchorOffsetY = 0;
		this.medalLayer.addChild(di);
		this.medalLayer.addChild(jinDu);
		/**进度显示 */
		let jinDuShow: eui.Label = this.loadLabel(min + "/" + max, 0, 91, 100, 10, 0, 10);
		this.medalLayer.addChild(jinDuShow);
		/**达成信息 */
		let AWord: eui.Label = this.loadLabel(this.introduce, 0, 69, 156, 12, 0, 12);
		this.medalLayer.addChild(AWord);
		/**勋章名字 */
		let medalName: eui.Label = this.loadLabel(this.medalName, 0, -83, (this.medalName.length + 2) * 24, 24, 0, 24);
		medalName.bold = true;//设置是否加粗
		medalName.italic = true;//设置是否斜体
		//设置描边属性
		medalName.strokeColor = 0x000000;
		medalName.stroke = 1;
		this.medalLayer.addChild(medalName);

	}

	/**获取到勋章但是没有点击领取 */
	public getMedalShow() {
		this.restAllLayer();
		let medal: eui.Image = this.loadImg(this.getMedalName, 0, 0, 90, 90);
		this.medalLayer.addChild(medal);
		medal.addEventListener(egret.TouchEvent.TOUCH_TAP, e => {
			DataProl.medal_main.changeUserSkin(this.shopId, this.shopType, this.states);
		}, this);
	}
	/**获取到成就皮肤显示 */
	public shopShow() {
		this.restAllLayer();
		//-------------------------------------加载商品饰品框
		let goodFrame: eui.Image = this.loadImg("shangpinkuang_1", 0, -10, 92, 92);
		this.skinLayer.addChild(goodFrame);
		//-------------------------------------加载商品图片
		if (this.shopType == 0) {
			let goodImg: eui.Image = this.loadImg(this.medalToShopName, 0, -10, 90, 40);
			this.skinLayer.addChild(goodImg);
		} else {
			let goodImg: eui.Image = this.loadImg(this.medalToShopName, 0, -10, 70, 90);
			this.skinLayer.addChild(goodImg);
		}
		//-------------------------------------加载商品文字
		let goodLab: eui.Label = this.loadLabel(this.shopName, 0, -83, (this.shopName.length + 2) * 24, 24, 0, 24);
		goodLab.bold = true;//设置是否加粗
		goodLab.italic = true;//设置是否斜体
		//设置描边属性
		goodLab.strokeColor = 0x000000;
		goodLab.stroke = 1;
		this.skinLayer.addChild(goodLab);
		//-------------------------------------加载商品点击状态
		let goodAction = this.states == 2 ? 0 : 1;
		let goodAct: eui.Image = this.loadImg(this.GoodsAction[goodAction], 0, 70, 120, 58);
		goodAct.addEventListener(egret.TouchEvent.TOUCH_TAP, e => {
			SoundManager.getInstance().playEffectSound("click_mp3");
			egret.Tween.get(goodAct).to({ scaleX: 0.7, scaleY: 0.7 }, 100).to({ scaleX: 1, scaleY: 1 }, 100);
			DataProl.medal_main.changeUserSkin(this.shopId, this.shopType, this.states);
		}, this);
		this.skinLayer.addChild(goodAct);
	}


}